<?php
/*
 * Copyright (c) 2011 Todd Brady
 *
 * PS3Theatre is freely distributable under the terms of the MIT license.
 * For details, see the MIT-Liscence web site: http://www.opensource.org/licenses/mit-license.php 
 */
 include_once('../includes/db_connect.php');
 
$movieId=$_GET["movieId"];
$backdrop=$_GET["backdrop"];
$medPoster=$_GET["medPoster"];
$cacheCompDir = $_SESSION['cacheCompDir'];
$cacheHttpDir = $_SESSION['cacheHttpDir'];
$subfolderPath = "Movies/";
$jsonString = '{';
$xml = '';
$posterSrc = '';
$backdropSrc = '';
$width=1920;
$comma=0;

$cacheQuery = mysql_query("SELECT c.id, c.type_id, c.link_id, c.keyword, c.subfolder_path, c.filename FROM tbl_cacheimage c,tbl_types t WHERE c.type_id=t.id AND t.type='Movies' and c.link_id=$movieId") or die(mysql_error());
$backdropRow = ''; 
$medPosterRow = '';

while ($row = mysql_fetch_array($cacheQuery)) {
	if($row['keyword'] == 'backdrop'){
		$backdropRow = $row;
	}else if($row['keyword'] == 'medPoster'){
		$medPosterRow = $row;
	}
} 

if($backdrop != ''){
	if($backdropRow != ''){
		//already cached, just print url
		if($comma != 0){
			$jsonString.=', ';
			$comma++;
		}
		
		$jsonString.='"backdropSrc" : "'.$cacheHttpDir.$backdropRow['subfolder_path'].$backdropRow['filename'].'"';
		
	}else{
		//not cached, need to get image, save, and put in cacheimage table
		if($movieId > -1){
			if($xml == ''){
				$xml = simplexml_load_file('http://api.themoviedb.org/2.1/Movie.getImages/en/xml/131bce11e82d2af21ed5378e7e52cba5/'.$movieId);
			}
			$backdrop = $xml->xpath("/OpenSearchDescription/movies/movie/images/backdrop");
			$poster = $xml->xpath("/OpenSearchDescription/movies/movie/images/poster");
			$i=0;
			while($i < count($backdrop) && $backdrop[$i]->image[0][width] != $width){
				$i++;
			}
	
			$posterSrc = $poster[0]->image[1][url];
			$backdropSrc = $backdrop[$i]->image[0][url];
			
			$ext = substr($backdropSrc,strrpos($backdropSrc,'.'),strlen($backdropSrc));
			$image = file_get_contents($backdropSrc);
			
			if(!is_dir ($cacheCompDir.$subfolderPath)){
				mkdir($cacheCompDir.$subfolderPath);
			}
			
			file_put_contents($cacheCompDir.$subfolderPath.$movieId.'-backdrop'.$ext, $image);
				
			if($comma != 0){
				$jsonString.=', ';
				$comma++;
			}
			
			$jsonString.='"backdropSrc" : "'.$cacheHttpDir.$subfolderPath.$movieId.'-backdrop'.$ext.'"';
			
			//insert record into cache image table
			$insertQuery = sprintf("INSERT INTO tbl_cacheimage (type_id,link_id,keyword,subfolder_path,filename) select id,%s,'%s','%s','%s' FROM tbl_types where type='%s'", 
									   mysql_real_escape_string($movieId),
									   mysql_real_escape_string('backdrop'),
									   mysql_real_escape_string($subfolderPath),
									   mysql_real_escape_string($movieId.'-backdrop'.$ext),
									   mysql_real_escape_string('Movies'));
	
			//insert movie record
			mysql_query($insertQuery) or die('error inserting backdrop record: query: '.$insertQuery.'error:'.mysql_error());
		}else{
			$jsonString.='"backdropSrc" : ""';
		}
	}
}

if($medPoster != ''){
	if($medPosterRow != ''){
		//already cached, just print url
		if($comma != 0){
			$jsonString.=', ';
			$comma++;
		}
		
		$jsonString.='"posterSrc" : "'.$cacheHttpDir.$medPosterRow['subfolder_path'].$medPosterRow['filename'].'"';
	}else{
		//not cached, need to get image, save, and put in cacheimage table
		if($movieId > -1){
			if($xml == ''){
				$xml = simplexml_load_file('http://api.themoviedb.org/2.1/Movie.getImages/en/xml/131bce11e82d2af21ed5378e7e52cba5/'.$movieId);
			}
			$poster = $xml->xpath("/OpenSearchDescription/movies/movie/images/poster");
			$posterSrc = $poster[0]->image[1][url];
			
			$ext = substr($posterSrc,strrpos($posterSrc,'.'),strlen($posterSrc));
			$image = file_get_contents($posterSrc);
		
			if(!is_dir ($cacheCompDir.$subfolderPath)){
				mkdir($cacheCompDir.$subfolderPath);
			}
			
			file_put_contents($cacheCompDir.$subfolderPath.$movieId.'-medPoster'.$ext, $image);
			
			if($comma != 0){
				$jsonString.=', ';
				$comma++;
			}
			
			$jsonString.='"posterSrc" : "'.$cacheHttpDir.$subfolderPath.$movieId.'-medPoster'.$ext.'"';
			
			//insert record into cache image table
			$insertQuery = sprintf("INSERT INTO tbl_cacheimage (type_id,link_id,keyword,subfolder_path,filename) select id,%s,'%s','%s','%s' FROM tbl_types where type='%s'", 
									   mysql_real_escape_string($movieId),
									   mysql_real_escape_string('medPoster'),
									   mysql_real_escape_string($subfolderPath),
									   mysql_real_escape_string($movieId.'-medPoster'.$ext),
									   mysql_real_escape_string('Movies'));
	
			//insert movie record
			mysql_query($insertQuery) or die('error inserting med poster record: query: '.$insertQuery.'error:'.mysql_error());
		}else{
			$jsonString.='"posterSrc" : ""';
		}
	}

}

	echo $jsonString.'}';
?>